package com.kkd.kkdapp.activity;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.adapter.DebtDetailViewPagerAdapter;
import com.kkd.kkdapp.adapter.DebtDetailsAdapter;
import com.kkd.kkdapp.base.BaseActivity;
import com.kkd.kkdapp.http.entity.BillListData;
import com.kkd.kkdapp.http.entity.CancelDebtData;
import com.kkd.kkdapp.http.entity.PoiInfoData;
import com.kkd.kkdapp.http.entity.RegularDebtDetailsData;
import com.kkd.kkdapp.http.entity.TransInfoData;
import com.kkd.kkdapp.http.manager.HttpCallBack;
import com.kkd.kkdapp.http.manager.KkdHttpClient;
import com.kkd.kkdapp.http.manager.KkdHttpMagager;
import com.kkd.kkdapp.listener.OnSingleClickListener;
import com.kkd.kkdapp.utils.AppUtil;
import com.kkd.kkdapp.utils.DialogUtils;
import com.kkd.kkdapp.view.KkdXlistView;
import com.kkd.kkdapp.view.KkdXlistView.KKDScrollListener;

/**
 * 债权转让详情页
 */
public class MyDebtDetailActivity extends BaseActivity implements
		KKDScrollListener {

	private Context mContext = this;

	private final static int SUC_REQUEST = 1001;
	private final static int FAIL_REQUEST = 1002;
	private final static int SUC_CANCEL = 2001;
	private final static int FAIL_CANCEL = 2002;

	private final static int TYPE_CHIYOUZHONG1 = 18881;
	private final static int TYPE_CHIYOUZHONG2 = 18882;
	private final static int TYPE_ZHUANRANGZHONG = 18883;
	private final static int TYPE_YIZHUANRANG = 18884;

	private LayoutInflater inflater;
	private View back;
	private TextView td_top_tv;
	private TextView agreement_bt;
	private TextView bt_bottom;
	private ImageView id_no_content;
	private TextView id_no_content_text;
	private KkdXlistView mXListView;
	private ListView mListView;

	private View headView;
	private ViewPager viewpager;
	private View view_left;
	private View view_right;
	private ImageView point1;
	private ImageView point2;
	private List<View> list_views = new ArrayList<View>();
	private DebtDetailViewPagerAdapter pagerAdapter;
	private TextView bid_name_head;
	private TextView bid_state_head;
	private View heat_to;
	private TextView title_left_head;
	private TextView tv_left_head;
	private TextView title_mid_head;
	private TextView tv_mid_head;
	private TextView title_right_head;
	private TextView tv_right_head;
	private View tip_left_head;
	private View toDetail;
	private TextView title_viewpager_title;
	private View linear_viewpager;
	private View linear_no_viewpager;
	private View goumaijiage_no_viewpager;
	private TextView tv_parts_no_viewpager;
	private TextView tv_price_no_viewpager;
	private TextView tv_jiazhi_no_viewpager;
	private TextView tv_yujizhuande_no_viewpager;
	private TextView tv_nianhuashouyi_no_viewpager;
	private TextView tv_qixian_no_viewpager;
	private TextView tv_touziriqi_no_viewpager;
	private View tip_null_billlist;
	private TextView tv_tip_billlis;

	private DebtDetailsAdapter adapter;
	private List<BillListData> billList = new ArrayList<BillListData>();

	private Dialog dialog_cancelDebt;
	private Dialog dialog_success_cancelDebt;
	private PopupWindow tip_popupWindow;
	private Dialog loadDialog;
	private DecimalFormat df = new DecimalFormat("###,##0.00");
	private DecimalFormat df_point2 = new DecimalFormat("0.00");

	/**
	 * 当前类型
	 */
	private int curType;
	private int type;

	private String bidId;
	private String poiId;
	private String transId;
	private String poiTransId = "";
	private int isFirst;// 是否为一手债权
	private int isTrans;// 是否可转让 0-否，1-是
	private String agreeloanUrl;// 债权转让协议
	private String docUrl;
	private String xyDesc;
	private String contractNo;

	private String bidTitle;
	private int remainParts;// 当前剩余
	private double waitCollectAmount;// 待收本息
	private String appbiddetail;// 详情页h5url
	private boolean isShowToastOnRefresh = false;
	public static MyDebtDetailActivity mActivity;
	private boolean isCanCancelDebt = true;

	private Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case SUC_REQUEST:
				RegularDebtDetailsData detailsData = (RegularDebtDetailsData) msg.obj;
				if (detailsData != null) {
					isFirst = detailsData.getIsFirst();
					if (curType == TYPE_CHIYOUZHONG1) {
						// 根据详情页接口返回的isFirst判断重新curType
						if (isFirst == 1) {
							curType = TYPE_CHIYOUZHONG1;
						} else {
							curType = TYPE_CHIYOUZHONG2;
						}
						if (curType == TYPE_CHIYOUZHONG1) {// 一手债权隐藏购买价格栏目
							goumaijiage_no_viewpager.setVisibility(View.GONE);
						} else if (curType == TYPE_CHIYOUZHONG2) {// 二手债权展示购买价格栏目
							goumaijiage_no_viewpager
									.setVisibility(View.VISIBLE);
						}
					}

					// 持有中一手债权右上角协议名称为 ---合同与协议
					if (curType == TYPE_CHIYOUZHONG1) {
						agreement_bt.setText("合同与协议");
					}

					initTitle();

					if (!TextUtils.isEmpty(detailsData.getPoiId())) {
						poiId = detailsData.getPoiId();
					}
					if (!TextUtils.isEmpty(detailsData.getBidId())) {
						bidId = detailsData.getBidId();
					}
					if (!TextUtils.isEmpty(detailsData.getTransId())) {
						transId = detailsData.getTransId();
					}
					if (!TextUtils.isEmpty(detailsData.getPoiTransId())) {
						poiTransId = detailsData.getPoiTransId();
					}
					bidTitle = detailsData.getBidTitle();
					isTrans = detailsData.getIsTrans();
					agreeloanUrl = detailsData.getAgreeloanUrl();
					docUrl = detailsData.getDocUrl();
					xyDesc = detailsData.getXyDesc();
					contractNo = detailsData.getContractNo();
					appbiddetail = detailsData.getAppbiddetail();
					if (!TextUtils.isEmpty(xyDesc)
							&& curType != TYPE_CHIYOUZHONG1) {
						agreement_bt.setText(xyDesc);
					}
					setHeadViewData(detailsData);

					billList.clear();
					if (type != 3) {// 已转让不展示回款明细
						billList.addAll(detailsData.getBillList());
					}
					if (adapter == null) {
						adapter = new DebtDetailsAdapter(mContext, billList,
								type);
						mListView.setAdapter(adapter);
					} else {
						adapter.notifyDataSetChanged();
					}

					id_no_content.setVisibility(View.GONE);
					id_no_content_text.setVisibility(View.GONE);
					if (billList == null || billList.size() == 0) {
						tip_null_billlist.setVisibility(View.VISIBLE);
						if (type == 3) {// 已转让
							tv_tip_billlis.setText("您的债权已转让成功，请在持有中查看回款明细");
						} else {
							tv_tip_billlis.setText("暂无回款计划，将在该标的放款后生成");
						}
					} else {
						tip_null_billlist.setVisibility(View.GONE);
					}
					mXListView.stopRefresh();
					mXListView.initState();
					if (!isShowToastOnRefresh) {
						isShowToastOnRefresh = true;
					}
				}
				break;

			case FAIL_REQUEST:
				if ((billList == null || billList.size() == 0)
						&& adapter == null) {
					id_no_content.setImageResource(R.drawable.img_null_net);
					if (msg.arg1 == KkdHttpClient.NO_NET) {
						id_no_content_text.setText("网络错误，请稍候下拉刷新重试");
					} else {
						id_no_content_text.setText("服务异常，请稍候下拉刷新重试");
					}
					id_no_content.setVisibility(View.VISIBLE);
					id_no_content_text.setVisibility(View.VISIBLE);
				}
				if (isShowToastOnRefresh) {
					String desc = (String) msg.obj;
					if (!TextUtils.isEmpty(desc)) {
						AppUtil.makeToastForCustomer(mContext, desc);
					}
				} else {
					isShowToastOnRefresh = true;
				}
				mXListView.stopRefresh();
				mXListView.initState();
				break;

			case SUC_CANCEL:
				if (loadDialog != null) {
					loadDialog.cancel();
					loadDialog = null;
				}
				showSuccessForCancelDebaDailog(bidTitle);
				break;

			case FAIL_CANCEL:
				if (loadDialog != null) {
					loadDialog.cancel();
					loadDialog = null;
				}
				String desc_canceldebt = (String) msg.obj;
				if (!TextUtils.isEmpty(desc_canceldebt)) {
					AppUtil.makeToastForCustomer(mContext, desc_canceldebt);
				}
				break;

			default:
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_debt_detail);
		mActivity = this;
		getMessage();
		initView();
		initListener();
		requestDetailData();
	}

	private void getMessage() {
		Intent intent = getIntent();
		bidId = intent.getStringExtra("bidId");
		poiId = intent.getStringExtra("poi_id");
		transId = intent.getStringExtra("transId");
		type = intent.getIntExtra("type", 1);
		if (type == 1 || type == 4) {
			// 先默认是持有中一手债权，之后在详情页接口里根据isFirst重新判断是TYPE_CHIYOUZHONG1还是TYPE_CHIYOUZHONG2
			curType = TYPE_CHIYOUZHONG1;
		} else if (type == 2) {
			curType = TYPE_ZHUANRANGZHONG;
		} else if (type == 3) {
			curType = TYPE_YIZHUANRANG;
		}
	}

	/**
	 * 根据标的类型重新设置页面标题
	 */
	private void initTitle() {
		if (type == 1 || type == 4) {
			if (isFirst == 1) {
				td_top_tv.setText("投资详情");
			} else {
				td_top_tv.setText("债权投资详情");
			}
		} else if (type == 2) {
			td_top_tv.setText("转让详情");
		} else if (type == 3) {
			td_top_tv.setText("转让详情");
		}
	}

	private void initView() {
		inflater = LayoutInflater.from(mContext);
		// 返回键
		back = findViewById(R.id.td_top_img);
		// 标题名称
		td_top_tv = (TextView) findViewById(R.id.td_top_tv);
		// 债权转让协议
		agreement_bt = (TextView) findViewById(R.id.agreement_bt);
		// 撤回剩余债权按钮
		bt_bottom = (TextView) findViewById(R.id.bt_bottom);
		// 空页面内容
		id_no_content = (ImageView) findViewById(R.id.id_no_content);
		id_no_content_text = (TextView) findViewById(R.id.id_no_content_text);
		// XLISTVIEW
		mXListView = (KkdXlistView) findViewById(R.id.mXListView);
		mXListView.setKKDScrollListener(this);
		mListView = mXListView.mListView;
		mListView.setDividerHeight(0);
		initHeadview();
		mListView.addHeaderView(headView);
		mXListView.startRefresh();
	}

	/**
	 * 初始化头部view
	 */
	@SuppressWarnings("deprecation")
	private void initHeadview() {
		headView = inflater.inflate(R.layout.debtdetails_listview_head, null);
		// 无回款计划的空页面提示
		tip_null_billlist = headView.findViewById(R.id.tip_null_billlist);
		tv_tip_billlis = (TextView) headView.findViewById(R.id.tv_tip_billlis);
		// 标题栏
		heat_to = headView.findViewById(R.id.heat_to);
		// 标题名称
		bid_name_head = (TextView) headView.findViewById(R.id.bid_name);
		// 标题状态
		bid_state_head = (TextView) headView.findViewById(R.id.bid_state);
		// 左边灰色标题
		title_left_head = (TextView) headView
				.findViewById(R.id.title_left_head);
		// 左边灰色提示
		tip_left_head = headView.findViewById(R.id.tip_left_head);
		// 左边灰色数值
		tv_left_head = (TextView) headView.findViewById(R.id.tv_left_head);
		// 中间灰色标题
		title_mid_head = (TextView) headView.findViewById(R.id.title_mid_head);
		// 中间灰色数值
		tv_mid_head = (TextView) headView.findViewById(R.id.tv_mid_head);
		// 右边灰色标题
		title_right_head = (TextView) headView
				.findViewById(R.id.title_right_head);
		// 右边灰色数值
		tv_right_head = (TextView) headView.findViewById(R.id.tv_right_head);
		// 中部投资信息或者转让信息栏目点击跳转
		toDetail = headView.findViewById(R.id.toDetail);
		// 中部的标题
		title_viewpager_title = (TextView) headView
				.findViewById(R.id.title_viewpager_title);
		/*
		 * 不包含viewpager的容器
		 */
		linear_no_viewpager = headView.findViewById(R.id.linear_no_viewpager);
		// 购买价格栏目
		goumaijiage_no_viewpager = headView
				.findViewById(R.id.goumaijiage_no_viewpager);
		// 购买份数
		tv_parts_no_viewpager = (TextView) headView
				.findViewById(R.id.tv_parts_no_viewpager);
		// 购买价格
		tv_price_no_viewpager = (TextView) headView
				.findViewById(R.id.tv_price_no_viewpager);
		// 购买价值
		tv_jiazhi_no_viewpager = (TextView) headView
				.findViewById(R.id.tv_jiazhi_no_viewpager);
		// 本金000.00元，预计赚得收益00.00元
		tv_yujizhuande_no_viewpager = (TextView) headView
				.findViewById(R.id.tv_yujizhuande_no_viewpager);
		// 预期年化收益
		tv_nianhuashouyi_no_viewpager = (TextView) headView
				.findViewById(R.id.tv_nianhuashouyi_no_viewpager);
		// 投资期限
		tv_qixian_no_viewpager = (TextView) headView
				.findViewById(R.id.tv_qixian_no_viewpager);
		// 投资日期
		tv_touziriqi_no_viewpager = (TextView) headView
				.findViewById(R.id.tv_touziriqi_no_viewpager);
		/*
		 * 包含viewpager的容器
		 */
		linear_viewpager = headView.findViewById(R.id.linear_viewpager);
		// 圆点
		point1 = (ImageView) headView.findViewById(R.id.point1);
		point2 = (ImageView) headView.findViewById(R.id.point2);
		// viewpager
		viewpager = (ViewPager) headView.findViewById(R.id.viewpager);
		/*
		 * 重新设置中部的标题以及设置展示哪个容器
		 */
		if (curType == TYPE_CHIYOUZHONG1 || curType == TYPE_CHIYOUZHONG2) {// 持有中一二手债权
			title_viewpager_title.setText("投资信息");
			linear_viewpager.setVisibility(View.GONE);
			linear_no_viewpager.setVisibility(View.VISIBLE);
		} else {// 转让中，已转让
			title_viewpager_title.setText("转让信息");
			linear_viewpager.setVisibility(View.VISIBLE);
			linear_no_viewpager.setVisibility(View.GONE);
			// 设置viewpager
			pagerAdapter = new DebtDetailViewPagerAdapter(list_views);
			viewpager.setAdapter(pagerAdapter);
			viewpager.setOnPageChangeListener(new OnPageChangeListener() {

				@Override
				public void onPageSelected(int position) {
					if (position == 0) {
						point1.setImageResource(R.drawable.point_true);
						point2.setImageResource(R.drawable.point_false);
						title_viewpager_title.setText("转让信息");
					} else {
						point1.setImageResource(R.drawable.point_false);
						point2.setImageResource(R.drawable.point_true);
						title_viewpager_title.setText("投资信息");
					}
				}

				@Override
				public void onPageScrolled(int arg0, float arg1, int arg2) {

				}

				@Override
				public void onPageScrollStateChanged(int arg0) {

				}
			});
		}
	}

	/**
	 * 给headview设置数据
	 */
	private void setHeadViewData(RegularDebtDetailsData detailsData) {
		if (curType == TYPE_CHIYOUZHONG1 || curType == TYPE_CHIYOUZHONG2) {
			if (type == 1) {
				title_mid_head.setText("剩余期限");
				bt_bottom.setText("转让债权");
				if (isTrans == 0) {
					bt_bottom.setBackgroundColor(Color.parseColor("#DBDBDB"));
				} else {
					bt_bottom.setBackgroundColor(Color.parseColor("#635FBD"));
				}
				bt_bottom.setVisibility(View.VISIBLE);
			} else if (type == 4) {
				title_mid_head.setText("投资期限");
				bt_bottom.setVisibility(View.GONE);
			}
			title_left_head.setText("投资金额");
			tip_left_head.setVisibility(View.GONE);
			title_right_head.setText("持有份数");

			// 标题
			bid_name_head.setText(detailsData.getBidTitle());
			// 投资金额
			tv_left_head
					.setText(df.format(detailsData.getPoiAmountLong() * 0.01)
							+ "元");
			if (type == 1) {// 剩余期限
				tv_mid_head.setText(detailsData.getRemainTerm());
				// 标的状态
				if (curType == TYPE_CHIYOUZHONG1) {
					String bidStatusCode = detailsData.getBidStatusCode();
					if ("5002".equals(bidStatusCode)) {
						bid_state_head
								.setBackgroundResource(R.drawable.bidding_bg);
						bid_state_head.setText("招标中");
					} else if ("5003".equals(bidStatusCode)) {
						bid_state_head
								.setBackgroundResource(R.drawable.bidding_bg);
						bid_state_head.setText("已满标");
					} else if ("5004".equals(bidStatusCode)) {
						bid_state_head
								.setBackgroundResource(R.drawable.repaymentin_bg);
						bid_state_head.setText("还款中");
					} else if ("5005".equals(bidStatusCode)) {
						bid_state_head
								.setBackgroundResource(R.drawable.haspaidoff_bg);
						bid_state_head.setText("已还清");
					}
				} else {
					bid_state_head
							.setBackgroundResource(R.drawable.repaymentin_bg);
					bid_state_head.setText("还款中");
				}
			} else if (type == 4) {// 投资期限
				tv_mid_head.setText(detailsData.getPoiTerm());
				// 标的状态
				bid_state_head.setBackgroundResource(R.drawable.haspaidoff_bg);
				bid_state_head.setText("已还清");
			}

			// 持有份数
			tv_right_head.setText(detailsData.getPoiParts() + "份");
			// 投资信息
			PoiInfoData poiInfo = detailsData.getPoiInfo();
			if (poiInfo != null) {
				// 设置购买份数
				tv_parts_no_viewpager.setText(poiInfo.getPoiParts() + "份");
				// 设置购买价格
				if (curType == TYPE_CHIYOUZHONG2) {
					tv_price_no_viewpager.setText(df.format(poiInfo
							.getPartPrice() * 0.01) + "元/份");
				}
				// 设置购买价值
				tv_jiazhi_no_viewpager
						.setText(df.format(poiInfo.getPartValue() * 0.01)
								+ "元/份");
				// 例：本金xx元，预计赚得收益xx元
				tv_yujizhuande_no_viewpager.setText(poiInfo.getPoiDesc());
				// 设置预期年化收益率
				tv_nianhuashouyi_no_viewpager.setText(poiInfo
						.getExpectInterest());
				// 设置投资期限
				tv_qixian_no_viewpager.setText(poiInfo.getPoiTerm());
				// 设置投资日期
				tv_touziriqi_no_viewpager.setText(poiInfo.getPoiTime());
			}
		} else {
			list_views.clear();
			if (curType == TYPE_YIZHUANRANG) {
				bt_bottom.setVisibility(View.GONE);
				title_left_head.setText("成交金额");
				tip_left_head.setVisibility(View.VISIBLE);
				title_mid_head.setText("成交份数");
				title_right_head.setText("转让系数");

				// 标题
				bid_name_head.setText(detailsData.getBidTitle());
				// 成交金额
				tv_left_head
						.setText(df.format(detailsData.getFinishAmount() * 0.01)
								+ "元");
				// 成交份数
				tv_mid_head.setText(detailsData.getFinishParts() + "份");
				// 转让系数
				tv_right_head.setText(detailsData.getTransFactor() + "%");
				// 标的状态
				bid_state_head.setBackgroundResource(R.drawable.bidding_bg);
				bid_state_head.setText("已转完");
				tip_left_head.setOnClickListener(new OnSingleClickListener() {

					@Override
					public void onSingleClick(View v) {
						showTipPopupwindow(v, "已转让的债权总额，未扣除服务费");
					}
				});
				// 给viewpager左半部分添加数据
				view_left = LayoutInflater.from(mContext).inflate(
						R.layout.item_debtdetail_left, null);
				// 债权ID
				TextView tv_id = (TextView) view_left.findViewById(R.id.tv_id);
				// 服务费扣除
				TextView tv_fuwufei = (TextView) view_left
						.findViewById(R.id.tv_fuwufei);
				// 服务费扣除tip
				View tip_left = view_left.findViewById(R.id.tip_left);
				// 预计转让份数
				TextView tv_fenshu = (TextView) view_left
						.findViewById(R.id.tv_fenshu);
				// 预计转让金额
				TextView tv_jine = (TextView) view_left
						.findViewById(R.id.tv_jine);
				// 成交完成
				TextView tv_date = (TextView) view_left
						.findViewById(R.id.tv_date);

				// 转让信息
				TransInfoData transInfo = detailsData.getTransInfo();
				final int serviceFeeRatio;// 预期服务费比率
				if (transInfo != null) {
					// 债权ID
					tv_id.setText(transInfo.getDebtId());
					// 服务费扣除
					tv_fuwufei
							.setText(df.format(transInfo.getServiceFee() * 0.01)
									+ "元");
					// 预计转让份数
					tv_fenshu.setText(transInfo.getExpectParts() + "份");
					// 预计转让金额
					tv_jine.setText(df.format(transInfo.getExpectTransAmount() * 0.01)
							+ "元");
					// 成交完成时间
					tv_date.setText(transInfo.getLastTransTime());
					// 预期服务费比率
					serviceFeeRatio = transInfo.getServiceFeeRatio();
				} else {
					serviceFeeRatio = 100;
				}
				tip_left.setOnClickListener(new OnSingleClickListener() {

					@Override
					public void onSingleClick(View v) {
						showTipPopupwindow(
								v,
								"根据成交金额的"
										+ (df_point2
												.format(1.00 * serviceFeeRatio / 100))
										+ "%收取");
					}
				});

				list_views.add(view_left);
			} else if (curType == TYPE_ZHUANRANGZHONG) {
				isCanCancelDebt = detailsData.getRemainParts() > 0;
				bt_bottom.setBackgroundColor(Color
						.parseColor(isCanCancelDebt ? "#6AB31C" : "#DBDBDB"));
				bt_bottom.setText("撤回剩余债权");
				bt_bottom.setVisibility(View.VISIBLE);
				title_left_head.setText("预期转让金额");
				tip_left_head.setVisibility(View.GONE);
				title_mid_head.setText("剩余期限");
				title_right_head.setText("剩余份数");

				// 标题
				bid_name_head.setText(detailsData.getBidTitle());
				// 预期转让金额
				tv_left_head.setText(df.format(detailsData
						.getExpectTransAmount() * 0.01) + "元");
				// 剩余期限
				tv_mid_head.setText(detailsData.getRemainTerm());
				// 剩余份数
				tv_right_head.setText(detailsData.getRemainParts() + "份");
				// 标的状态
				bid_state_head.setBackgroundResource(R.drawable.bid_finish_bg);
				bid_state_head.setText("转让中");
				// 给viewpager添加数据
				view_left = LayoutInflater.from(mContext).inflate(
						R.layout.item_debtdetail_left_ing, null);
				// 债权ID
				TextView tv_id_ing = (TextView) view_left
						.findViewById(R.id.tv_id_ing);
				// 成交份数
				TextView tv_fenshu_left_ing = (TextView) view_left
						.findViewById(R.id.tv_fenshu_left_ing);
				// 成交金额tip
				View tip_left_chengjiaojine_ing = view_left
						.findViewById(R.id.tip_left_chengjiaojine_ing);
				// 成交金额
				TextView tv_chengjiaojine_ing = (TextView) view_left
						.findViewById(R.id.tv_chengjiaojine_ing);
				// 服务费扣除tip
				View tip_left_fuwufei_ing = view_left
						.findViewById(R.id.tip_left_fuwufei_ing);
				// 服务费扣除
				TextView tv_fuwufei_ing = (TextView) view_left
						.findViewById(R.id.tv_fuwufei_ing);
				// 预计转让份数
				TextView tv_fenshu_ing = (TextView) view_left
						.findViewById(R.id.tv_fenshu_ing);
				// 转让系数
				TextView tv_jine_ing = (TextView) view_left
						.findViewById(R.id.tv_jine_ing);
				// 最后转让日
				TextView tv_date_ing = (TextView) view_left
						.findViewById(R.id.tv_date_ing);

				// 转让信息
				TransInfoData transInfo = detailsData.getTransInfo();
				final int serviceFeeRatio;// 预期服务费比率
				if (transInfo != null) {
					// 债权id
					tv_id_ing.setText(transInfo.getDebtId());
					// 成交份数
					tv_fenshu_left_ing
							.setText(transInfo.getFinishParts() + "份");
					// 成交金额
					tv_chengjiaojine_ing.setText(df.format(transInfo
							.getFinishAmount() * 0.01) + "元");
					// 服务费扣除
					tv_fuwufei_ing
							.setText(df.format(transInfo.getServiceFee() * 0.01)
									+ "元");
					// 预计转让份数
					tv_fenshu_ing.setText(transInfo.getExpectParts() + "份");
					// 转让系数
					tv_jine_ing.setText(df_point2.format(transInfo
							.getTransFactor() * 0.01) + "%");
					// 最后转让日
					tv_date_ing.setText(transInfo.getLastTransTime());
					// 预期服务费比率
					serviceFeeRatio = transInfo.getServiceFeeRatio();
				} else {
					serviceFeeRatio = 100;
				}
				tip_left_chengjiaojine_ing
						.setOnClickListener(new OnSingleClickListener() {

							@Override
							public void onSingleClick(View v) {
								showTipPopupwindow(v, "已转让的债权总额，未扣除服务费");
							}
						});
				tip_left_fuwufei_ing
						.setOnClickListener(new OnSingleClickListener() {

							@Override
							public void onSingleClick(View v) {
								showTipPopupwindow(
										v,
										"根据成交金额的"
												+ (df_point2
														.format(1.00 * serviceFeeRatio / 100))
												+ "%收取");
							}
						});

				list_views.add(view_left);
			}
			// 给viewpager右半部分添加数据
			view_right = LayoutInflater.from(mContext).inflate(
					R.layout.item_debtdetail_right, null);
			// 持有份数
			TextView tv_fenshu_right = (TextView) view_right
					.findViewById(R.id.tv_fenshu_right);
			// 购买价格
			TextView tv_price_right = (TextView) view_right
					.findViewById(R.id.tv_price_right);
			// 购买价格容器
			View tv_price_right_parent = view_right
					.findViewById(R.id.tv_price_right_parent);
			// 购买价值
			TextView tv_jiazhi_right = (TextView) view_right
					.findViewById(R.id.tv_jiazhi_right);
			// 中间文字描述
			TextView tv_yujizhuande = (TextView) view_right
					.findViewById(R.id.tv_yujizhuande);
			// 预期年化收益
			TextView tv_nianhuashouyi_right = (TextView) view_right
					.findViewById(R.id.tv_nianhuashouyi_right);
			// 投资期限
			TextView tv_qixian_right = (TextView) view_right
					.findViewById(R.id.tv_qixian_right);
			// 投资日期
			TextView tv_touziriqi_right = (TextView) view_right
					.findViewById(R.id.tv_touziriqi_right);

			// 一手债权不显示购买价格，二手债权显示购买价格
			if (isFirst == 1) {
				tv_price_right_parent.setVisibility(View.GONE);
			} else {
				tv_price_right_parent.setVisibility(View.VISIBLE);
			}
			// 投资信息
			PoiInfoData poiInfo = detailsData.getPoiInfo();
			if (poiInfo != null) {
				// 持有份数
				tv_fenshu_right.setText(poiInfo.getPoiParts() + "份");
				// 非一手债权购买价格
				if (isFirst != 1) {
					tv_price_right
							.setText(df.format(poiInfo.getPartPrice() * 0.01)
									+ "元/份");
				}
				// 购买价值
				tv_jiazhi_right
						.setText(df.format(poiInfo.getPartValue() * 0.01)
								+ "元/份");
				// 例：本金xx元，预计赚得收益xx元
				// tv_yujizhuande.setText(poiInfo.getPoiDesc());
				// 预期年化收益
				tv_nianhuashouyi_right.setText(poiInfo.getExpectInterest());
				// 投资期限
				tv_qixian_right.setText(poiInfo.getPoiTerm());
				// 投资日期
				tv_touziriqi_right.setText(poiInfo.getPoiTime());

				list_views.add(view_right);
			}

			pagerAdapter.notifyDataSetChanged();

			if (pagerAdapter.getCount() == 2) {
				viewpager.setCurrentItem(0);
			}
		}
	}

	private void initListener() {
		// 返回键
		back.setOnClickListener(new OnSingleClickListener() {

			@Override
			public void onSingleClick(View v) {
				finish();
			}
		});
		// 债权转让协议
		agreement_bt.setOnClickListener(new OnSingleClickListener() {

			@Override
			public void onSingleClick(View v) {
				if (curType == TYPE_CHIYOUZHONG1
						|| curType == TYPE_CHIYOUZHONG2) {// 只有一份协议，直接跳转到H5页
					if (!TextUtils.isEmpty(agreeloanUrl)) {
						Intent intent = new Intent(mContext,
								KkdPdfWebActivity.class);
						intent.putExtra("url", agreeloanUrl);
						intent.putExtra("pdfUrl", docUrl);
						intent.putExtra("fileName", "【KKD】" + xyDesc + "_"
								+ contractNo + ".pdf");
						startActivity(intent);
					}
				} else {// 可能有多份，跳转到列表页，然后再跳转到H5页
					Intent intent = new Intent(mContext,
							MyPlanAgreementActivity.class);
					intent.putExtra("transferId", transId);
					startActivity(intent);
				}
			}
		});
		toDetail.setOnClickListener(new OnSingleClickListener() {

			@Override
			public void onSingleClick(View v) {
				String curTitle = title_viewpager_title.getText().toString()
						.trim();
				if ("转让信息".equals(curTitle)) {// 跳转到债权详情页
					Intent intent = new Intent(mContext,
							CreditorIsRightsParticularsActivity.class);
					intent.putExtra("bidId", bidId);
					intent.putExtra("poiId", poiId);
					intent.putExtra("transferId", transId);
					startActivity(intent);
				} else if ("投资信息".equals(curTitle)) {
					if (isFirst == 1) {// 一手，跳转到标的详情页
						Intent intent = new Intent(mContext,
								RegularParticularsActivity.class);
						intent.putExtra("bidId", bidId);
						intent.putExtra("poiId", poiId);
						intent.putExtra("appbiddetail", appbiddetail);
						startActivity(intent);
					} else {// 二手，跳转到债权详情页
						Intent intent = new Intent(mContext,
								CreditorIsRightsParticularsActivity.class);
						intent.putExtra("bidId", bidId);
						intent.putExtra("poiId", poiId);
						intent.putExtra("transferId", poiTransId);
						startActivity(intent);
					}
				}
			}
		});
		// 撤回债权转让
		bt_bottom.setOnClickListener(new OnSingleClickListener() {

			@Override
			public void onSingleClick(View v) {
				if (curType == TYPE_ZHUANRANGZHONG) {// 撤回债权转让
					if (isCanCancelDebt) {
						requestDataForcancelDebtTrans();
					}
				} else if ((curType == TYPE_CHIYOUZHONG1 || curType == TYPE_CHIYOUZHONG2)
						&& isTrans == 1) {// 转让债权
					Intent intent = new Intent(mContext,
							DebtExchangeActivity.class);
					intent.putExtra("bidId", bidId);
					intent.putExtra("poiId", poiId);
					startActivity(intent);
				}
			}
		});
	}

	@Override
	public void onRefresh() {
		requestDetailData();
	}

	@Override
	public void onLoadMore() {

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (dialog_cancelDebt != null) {
			dialog_cancelDebt.cancel();
			dialog_cancelDebt = null;
		}
		if (dialog_success_cancelDebt != null) {
			dialog_success_cancelDebt.cancel();
			dialog_success_cancelDebt = null;
		}
		if (loadDialog != null) {
			loadDialog.cancel();
			loadDialog = null;
		}
	}

	/**
	 * 弹出是否撤回债权对话框
	 */
	private void showCancelDebtDialog(String remainParts,
			String waitCollectAmount) {
		if (dialog_cancelDebt != null) {
			dialog_cancelDebt.cancel();
			dialog_cancelDebt = null;
		}
		dialog_cancelDebt = DialogUtils.cancelDebt(mContext, remainParts + "份",
				waitCollectAmount + "元", new OnSingleClickListener() {

					@Override
					public void onSingleClick(View v) {
						if (dialog_cancelDebt != null) {
							dialog_cancelDebt.cancel();
							dialog_cancelDebt = null;
						}
						cancelDebtTrans();
					}
				});
		dialog_cancelDebt.show();
	}

	/**
	 * 弹出撤回成功对话框
	 */
	private void showSuccessForCancelDebaDailog(String title) {
		if (dialog_success_cancelDebt != null) {
			dialog_success_cancelDebt.cancel();
			dialog_success_cancelDebt = null;
		}
		dialog_success_cancelDebt = DialogUtils.cancelDebtSuccess(mContext,
				title, new OnSingleClickListener() {

					@Override
					public void onSingleClick(View v) {
						if (dialog_success_cancelDebt != null) {
							dialog_success_cancelDebt.cancel();
							dialog_success_cancelDebt = null;
						}
					}
				});
		dialog_success_cancelDebt.setOnCancelListener(new OnCancelListener() {

			@Override
			public void onCancel(DialogInterface dialog) {
				Intent intent = new Intent(mContext,
						MyRegularDebtActivity.class);
				startActivity(intent);
				finish();
				try {
					MyRegularDebtActivity.mActivity.finish();
				} catch (Exception e) {

				}
			}
		});
		dialog_success_cancelDebt.show();
	}

	/**
	 * 弹出气泡提示popupwindow
	 * 
	 * @param targetView
	 * @param desc
	 */
	private void showTipPopupwindow(View targetView, String desc) {
		if (tip_popupWindow != null) {
			tip_popupWindow.dismiss();
			tip_popupWindow = null;
		}
		tip_popupWindow = DialogUtils.showTipPopupWindow(mContext, targetView,
				desc);
	}

	/**
	 * 请求详情页面数据
	 */
	private void requestDetailData() {
		String id;
		if (type == 1 || type == 4) {
			id = poiId;
		} else {
			id = transId;
		}
		KkdHttpMagager.getInstance().getMyRegularDebtDetailManager(id, type,
				"0", RegularDebtDetailsData.class, new HttpCallBack() {

					@Override
					public void onSuccess(int statusCode, Object responseBody) {
						Message msg = Message.obtain();
						msg.what = SUC_REQUEST;
						msg.obj = responseBody;
						mHandler.sendMessage(msg);
					}

					@Override
					public void onFailure(int statusCode, String desc) {
						Message msg = Message.obtain();
						msg.what = FAIL_REQUEST;
						msg.obj = desc;
						msg.arg1 = statusCode;
						mHandler.sendMessage(msg);
					}
				});
	}

	/**
	 * 请求撤销债权转让所需数据接口
	 */
	private void requestDataForcancelDebtTrans() {
		if (loadDialog != null) {
			loadDialog.cancel();
			loadDialog = null;
		}
		loadDialog = DialogUtils.toLoadDialog(mContext, "加载中...");
		KkdHttpMagager.getInstance().getCancelDebtTransData(transId,
				CancelDebtData.class, new HttpCallBack() {

					@Override
					public void onSuccess(int statusCode, Object responseBody) {
						if (loadDialog != null) {
							loadDialog.cancel();
							loadDialog = null;
						}
						CancelDebtData cancelDebtData = (CancelDebtData) responseBody;
						if (cancelDebtData != null) {
							remainParts = cancelDebtData.getRemainParts();
							waitCollectAmount = cancelDebtData
									.getWaitCollectAmount();
							showCancelDebtDialog(remainParts + "",
									df.format(waitCollectAmount * 0.01));
						}
					}

					@Override
					public void onFailure(int statusCode, String desc) {
						if (loadDialog != null) {
							loadDialog.cancel();
							loadDialog = null;
						}
						if (!TextUtils.isEmpty(desc)) {
							AppUtil.makeToastForCustomer(mContext, desc);
						}
					}
				});
	}

	/**
	 * 请求撤销债权转让接口
	 */
	private void cancelDebtTrans() {
		if (loadDialog != null) {
			loadDialog.cancel();
			loadDialog = null;
		}
		loadDialog = DialogUtils.toLoadDialog(mContext, "加载中...");
		KkdHttpMagager.getInstance().getCancelDebtTrans(transId, String.class,
				new HttpCallBack() {

					@Override
					public void onSuccess(int statusCode, Object responseBody) {
						mHandler.sendEmptyMessage(SUC_CANCEL);
					}

					@Override
					public void onFailure(int statusCode, String desc) {
						Message msg = Message.obtain();
						msg.what = FAIL_CANCEL;
						msg.obj = desc;
						mHandler.sendMessage(msg);
					}
				});
	}
}
